JSON/XML ডেটা ফেচ এবং প্রসেস করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - RestTemplate এর পরিচিতি
142

Spring Boot Client ব্যবহার করে JSON বা XML ডেটা ফেচ এবং প্রসেস করা অত্যন্ত সহজ এবং সুবিধাজনক। Spring-এর RestTemplate এবং WebClient API সরঞ্জাম ব্যবহার করে এই কাজটি সম্পন্ন করা যায়। নিচে বিস্তারিতভাবে JSON এবং XML ডেটা হ্যান্ডলিং নিয়ে আলোচনা করা হলো।


JSON ডেটা ফেচ এবং প্রসেস করা

  1. Dependency যোগ করুন:

    Maven প্রজেক্টের জন্য spring-boot-starter-web ডিপেনডেন্সি যোগ করতে হবে।

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  2. JSON ডেটা ফেচ করার উদাহরণ:

    RestTemplate-এর মাধ্যমে JSON ফেচ:

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    public class SpringBootClientApplication implements CommandLineRunner {
    
        private final RestTemplate restTemplate = new RestTemplate();
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootClientApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            String url = "https://jsonplaceholder.typicode.com/posts/1";
            Post post = restTemplate.getForObject(url, Post.class);
            System.out.println("Title: " + post.getTitle());
            System.out.println("Body: " + post.getBody());
        }
    }
    
    // POJO Class
    class Post {
        private Long id;
        private String title;
        private String body;
    
        // Getters and Setters
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
        public String getBody() { return body; }
        public void setBody(String body) { this.body = body; }
    }
    

    WebClient-এর মাধ্যমে JSON ফেচ:

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.reactive.function.client.WebClient;
    
    @SpringBootApplication
    public class SpringBootClientApplication implements CommandLineRunner {
    
        private final WebClient webClient = WebClient.create();
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootClientApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            String url = "https://jsonplaceholder.typicode.com/posts/1";
            Post post = webClient.get()
                                 .uri(url)
                                 .retrieve()
                                 .bodyToMono(Post.class)
                                 .block();
    
            System.out.println("Title: " + post.getTitle());
            System.out.println("Body: " + post.getBody());
        }
    }
    

XML ডেটা ফেচ এবং প্রসেস করা

XML ডেটা প্রসেস করার জন্য spring-boot-starter-xml বা jackson-dataformat-xml ডিপেনডেন্সি ব্যবহার করতে হবে।

  1. Dependency যোগ করুন:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>
    
  2. XML ডেটা ফেচ করার উদাহরণ:

    RestTemplate-এর মাধ্যমে XML ফেচ:

    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @SpringBootApplication
    public class SpringBootClientApplication implements CommandLineRunner {
    
        private RestTemplate restTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootClientApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            restTemplate = createRestTemplate();
            String url = "https://example.com/api/data.xml";
            Item item = restTemplate.getForObject(url, Item.class);
    
            System.out.println("Item Name: " + item.getName());
            System.out.println("Item Description: " + item.getDescription());
        }
    
        private RestTemplate createRestTemplate() {
            RestTemplate restTemplate = new RestTemplate();
            List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
            messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
            restTemplate.setMessageConverters(messageConverters);
            return restTemplate;
        }
    }
    
    @JacksonXmlRootElement(localName = "Item")
    class Item {
        private String name;
        private String description;
    
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
    }
    

    WebClient-এর মাধ্যমে XML ফেচ:

    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.reactive.function.client.WebClient;
    
    @SpringBootApplication
    public class SpringBootClientApplication implements CommandLineRunner {
    
        private final WebClient webClient = WebClient.create();
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootClientApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            String url = "https://example.com/api/data.xml";
            Item item = webClient.get()
                                 .uri(url)
                                 .retrieve()
                                 .bodyToMono(Item.class)
                                 .block();
    
            System.out.println("Item Name: " + item.getName());
            System.out.println("Item Description: " + item.getDescription());
        }
    }
    
    @JacksonXmlRootElement(localName = "Item")
    class Item {
        private String name;
        private String description;
    
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
    }
    

উল্লেখযোগ্য বিষয়

  1. JSON ডেটা প্রসেসিং অনেক সহজ কারণ Spring Boot এর মধ্যে JSON ডিফল্টভাবে সাপোর্ট করে।
  2. XML ডেটার জন্য Jackson DataFormat বা অন্য কোনো XML কনভার্টার ব্যবহার করতে হয়।
  3. RestTemplate তুলনায় WebClient আধুনিক এবং রিঅ্যাকটিভ প্রোগ্রামিং সাপোর্ট করে।

আপনার প্রজেক্টের প্রয়োজন অনুযায়ী JSON বা XML ডেটা প্রসেস করতে এই উদাহরণগুলো অনুসরণ করতে পারেন।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...